import numpy as np
import cv2
import matplotlib.pyplot as plt
import math


def imageFourierTransform(img):
	# 对图像进行二维fft变换
	f = np.fft.fft2(img)
	# 将低频成分搬移到图像中心
	fshift = np.fft.fftshift(f)

	# 显示幅度谱 log
	magnitude_spectrum = 20*np.log(np.abs(fshift)+1)

	return magnitude_spectrum

def imageRotate():
	rows = 800
	cols = 800
	img = np.zeros((rows, cols))
	img[200:600, 300:500] = 1

	rotation_mask = cv2.getRotationMatrix2D((cols/2,rows/2),-30,1)
	dst = cv2.warpAffine(img,rotation_mask,(cols,rows))

	img_fourier = imageFourierTransform(img)
	dst_fourier = imageFourierTransform(dst)

	plt.subplot(221),plt.imshow(img, cmap = 'gray')
	plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(222),plt.imshow(img_fourier, cmap = 'gray')
	plt.title('Input Image fourier'), plt.xticks([]), plt.yticks([])
	plt.subplot(223),plt.imshow(dst, cmap = 'gray')
	plt.title('ratate Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(224),plt.imshow(dst_fourier, cmap = 'gray')
	plt.title('ratate Image fourier'), plt.xticks([]), plt.yticks([])
	plt.show()
	

if __name__ == '__main__':
	imageRotate()